// Operation Cookies
const setCookie = (name,value,expiresHours) => {
  let cookieString = name+"="+escape(value);
  //判断是否设置过期时间
  if(expiresHours>0){
    var date=new Date();
    date.setTime(date.getTime()+expiresHours*3600*1000);
    cookieString=cookieString+"; expires="+date.toGMTString();
  }
  document.cookie=cookieString;
}

const getCookie = (name) => {
  const reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)');
  const arr = document.cookie.match(reg);
  if (arr) {
    return decodeURIComponent(arr[2]);
  } else {
    return null;
  }
}

const delCookie = ({ name, domain, path }) => {
  if (getCookie(name)) {
    document.cookie = name + '=; expires=Thu, 01-Jan-70 00:00:01 GMT'
  }
}

// Operation LocalStorage
const setLocalStorage = ({key, vaule}) => {
  return localStorage.setItem(key, JSON.stringify(vaule));
}

const getLocalStorage = (key) => {
  const value = JSON.parse(localStorage.getItem(key));
  return value;
}

const isUserLogin = (name) => {
  const cookie = getCookie(name);
  return cookie != null;
};

export {
  setCookie,
  getCookie,
  delCookie,
  setLocalStorage,
  getLocalStorage,
  isUserLogin
}
